package com.vancouver.knows.ui.activity.citynews;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.util.StringUtil;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.task.mark.CommentReportTaskMark;

/**
 * 举报界面
 * 
 * @author chenxunlin01
 * 
 */
public class CommentReportActivity extends BaseActivity implements IResultReceiver {

	@Bind(R.id.toolbar)
	Toolbar toolbar;
	@Bind(R.id.reportOne)
	RadioButton reportOne;
	@Bind(R.id.reportTwo)
	RadioButton reportTwo;
	@Bind(R.id.reportThree)
	RadioButton reportThree;
	@Bind(R.id.reportFour)
	RadioButton reportFour;

	private String content;
	private String targetId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_commentreport);
		init();
	}

	private void init() {
		toolbar.setTitle("");
		setSupportActionBar(toolbar);
		toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back));
		targetId = getIntent().getStringExtra("target_id");
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@OnCheckedChanged(R.id.reportOne)
	public void onReportOneChecked(boolean checked) {
		if (checked) {
			content = reportOne.getText().toString();
		}
	}

	@OnCheckedChanged(R.id.reportTwo)
	public void onReportTwoChecked(boolean checked) {
		if (checked) {
			content = reportTwo.getText().toString();
		}
	}

	@OnCheckedChanged(R.id.reportThree)
	public void onReportThreeChecked(boolean checked) {
		if (checked) {
			content = reportThree.getText().toString();
		}
	}

	@OnCheckedChanged(R.id.reportFour)
	public void onReportFourChecked(boolean checked) {
		if (checked) {
			content = reportFour.getText().toString();
		}
	}

	@OnClick(R.id.reportBtn)
	public void onReportBtnClick() {
		if (StringUtil.isEmptyString(content)) {
			Toast.makeText(this, R.string.report_content_empty, 200).show();
			return;
		}

		BusinessModule bModule = imContext.getBusinessModule();
		CommentReportTaskMark aTaskMark = bModule.getTaskMarkPool().getCommentReportTaskMark();
		if (aTaskMark.getTaskStatus() == ATaskMark.HANDLE_DOING) {
			return;
		}

		UserInfo userInfo = bModule.getCacheManager().getUserInfo();
		if (userInfo == null) {
			return;
		}

		bModule.getServiceWrapper().commentReport(this, aTaskMark, userInfo.getUserID(), targetId, "C", content);
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if (taskMark instanceof CommentReportTaskMark) {
			if (taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
				finish();

			} else if (taskMark.getTaskStatus() == ATaskMark.HANDLE_ERROR) {
				Toast.makeText(this, R.string.report_error, 200).show();
			}
		}
	}
}
